package com.zsl.springboot.bootdemo.controller;

import com.zsl.springboot.bootdemo.model.CodeModel;
import com.zsl.springboot.bootdemo.model.DataResult;
import com.zsl.springboot.bootdemo.service.ICodeService;
import com.zsl.springboot.bootdemo.service.IUserService;
import com.zsl.springboot.bootdemo.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@RestController
@Api(tags = "用户管理")
public class BaseController {
    //slf4j是日志一种接口
    private Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private IUserService userService;

    @Autowired
    private ICodeService codeService;

    @RequestMapping("/genCode")
    public DataResult genCode(String phoneNumber){
        try {
            String code = String.valueOf((int)((Math.random()*9+1)*100000));
            CodeModel codeModel = new CodeModel();
            codeModel.setTelephone(phoneNumber);
            codeModel.setCode(code);
            codeModel.setCreatetime(new Date());
            boolean status = codeService.insert(codeModel);
            if(status) {
                return new DataResult(20000,"发送成功",null);
            }else {
                return  new DataResult(0,"发送失败,请稍后重试",null);
            }
        }catch (Exception e){
            return  new DataResult(0,"发送失败,请稍后重试",null);
        }
    }

    @RequestMapping("/login")
    @ApiOperation(value = "用户登录",notes = "用户登录")
    public DataResult login(String logtelval, String loginpwd, HttpServletRequest request) {
        try {
            boolean login = userService.login(logtelval, MD5Utils.getPwd(loginpwd));
            if(login) {
                request.getSession().setAttribute("user",logtelval);
                return new DataResult(200,"登录成功",null);
            }else {
                return  new DataResult(0,"登录失败",null);
            }
        }catch (Exception e){
            return  new DataResult(0,"登录失败",null);
        }
    }

    @RequestMapping("/islogin")
    public DataResult islogin(HttpServletRequest request) {
        String telephone = (String) request.getSession().getAttribute("user");
        if (StringUtils.isNotEmpty(telephone)){
            return new DataResult(1,"已登录",telephone);
        }else {
            return new DataResult(0,"未登录",null);
        }
    }


    @RequestMapping("/register")
    public DataResult register(String phoneNumber,String veriCode, String passwd) {
        try {
            CodeModel codeModel = new CodeModel();
            codeModel.setCode(veriCode);
            codeModel.setTelephone(phoneNumber);
            codeModel.setStatus(0);
            CodeModel dbCodeModel = codeService.findCodeByTel(codeModel);
            if (null == dbCodeModel){
                return new DataResult(0,"当前验证码无效",null);
            }
            long s = (new Date().getTime() - dbCodeModel.getCreatetime().getTime()) / (1000 * 60);
            if (dbCodeModel.getStatus() == 1 || s>3){
                return new DataResult(0,"验证码过期",null);
            }
            dbCodeModel.setStatus(1);
            codeService.update(dbCodeModel);
            boolean isExsit = userService.login(phoneNumber,null);
            if (isExsit){
                return new DataResult(201,"用户已注册",null);
            }
            boolean login = userService.register(phoneNumber, MD5Utils.getPwd(passwd));
            if(login) {
                return new DataResult(1,"注册成功",null);
            }else {
                return  new DataResult(0,"注册失败",null);
            }
        }catch (Exception e){
            return  new DataResult(0,"注册失败",null);
        }
    }


}
